package com.test.controller;

import com.test.util.HTTPUtil;
import com.test.util.JsonUtil;
import com.test.util.XmlUtil;
import com.test.wechat.AesException;
import com.test.wechat.SHA1;
import com.test.wechat.XMLParse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    //    @RequestMapping("wechat.html")
//    @ResponseBody
//    public String test(String signature, String timestamp, String nonce, String echostr) {
//        try {
//            String sign = SHA1.getSHA1("abcdefg", timestamp, nonce, "");
//            if (signature.equals(sign)) {
//                return echostr;
//            }
//
//        } catch (AesException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
    static String APPID = "xxxxx";
    static String APPSECRET = "xxxxxx";
    static String TOKEN = "19_aXUJ8-MruDqcOWIKCHSeV7qhoXVi-TIpfBYUYJs0_jQF-ibh0aR_E2N75lHbDCH-s99bb6pcUpDJFHrVQORocUISDPrGn6C1Ms3xNqXFe-NudUom9rSnkmDIKLg3mEEp6idEZMZY5v4PW46RCIFaACAOUR";

    static String token = "19_LmEa1a-0ZsJXadyYVcUBbX3gQ2tmWWzSJQ35CZi7zNnJJBWAUortY0VXMqZNP22amDR53KiGNVilCCWLF8NkzDbg9WPolkmPXP04eACfw4jYrXhArofMpd1e6cx1Ox9Fu-5jzDa2pdLPMi4AULYhAAAGXK";

    public static void main(String[] args) {
//        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET;
//        System.out.println(HTTPUtil.httpPost(url,"","utf-8"));
        String menuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token;
        Menu m1 = new Menu("click", "菜单1", "001", null);
        Menu m21 = new Menu("scancode_waitmsg", "扫码", "0021", null);
        Menu m22 = new Menu("click", "搜索", "0022", null);
        List<Menu> sub = new ArrayList<>();
        sub.add(m21);
        sub.add(m22);
        Menu m2 = new Menu("", "菜单2", "002", sub);

        List<Menu> m = new ArrayList<>();
        m.add(m1);
        m.add(m2);
        Map<String, Object> data = new HashMap<>();
        data.put("button", m);
        System.out.println(HTTPUtil.httpPost(menuUrl, JsonUtil.getJSON(data), "utf-8"));
    }

    static String account = "gh_e5a5c7fb0735";

    //<xml><ToUserName><![CDATA[gh_e5a5c7fb0735]]></ToUserName><FromUserName><![CDATA[ovfUvt-ENKv0hkUOYI5ofZHO6_D0]]></FromUserName><CreateTime>1553227216</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[我是老孟]]></Content><MsgId>22236870888894197</MsgId></xml>
    @RequestMapping("wechat.html")
    @ResponseBody
    public String msg(HttpServletRequest request) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            System.out.println(builder);
            Map<String, Object> message = XmlUtil.doXMLParse(builder.toString());
            String m = message.get("Content").toString();
            m = m.replace("?", "。").replace("么", "").replace("吗", "").replace("谁", "小可爱").replace("你", "我");
            String reply = "<xml>\n" +
                    "  <ToUserName><![CDATA[" + message.get("FromUserName") + "]]></ToUserName>\n" +
                    "  <FromUserName><![CDATA[" + account + "]]></FromUserName>\n" +
                    "  <CreateTime>" + System.currentTimeMillis() + "</CreateTime>\n" +
                    "  <MsgType><![CDATA[text]]></MsgType>\n" +
                    "  <Content><![CDATA["+m+ "]]></Content>\n" +
                    "</xml>";
            return reply;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "ok";
    }
}
